home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdNamedAnchorProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  4.7 KB  |  167 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  */
  22.  
  23. var insertNew = true;
  24. var tagName = "anchor";
  25. var anchorElement = null;
  26. var originalName = "";
  27.  
  28. // dialog initialization code
  29. function Startup()
  30. {
  31.   if (!InitEditorShell())
  32.     return;
  33.  
  34.   gDialog.OkButton  = document.documentElement.getButton("accept");
  35.   gDialog.NameInput = document.getElementById("nameInput");
  36.  
  37.   // Get a single selected element of the desired type
  38.   anchorElement = editorShell.GetSelectedElement(tagName);
  39.  
  40.   if (anchorElement) {
  41.     // We found an element and don't need to insert one
  42.     insertNew = false;
  43.  
  44.     // Make a copy to use for AdvancedEdit
  45.     globalElement = anchorElement.cloneNode(false);
  46.     originalName = ConvertToCDATAString(anchorElement.name);
  47.   } else {
  48.     insertNew = true;
  49.     // We don't have an element selected, 
  50.     //  so create one with default attributes
  51.     anchorElement = editorShell.CreateElementWithDefaults(tagName);
  52.     if (anchorElement) {
  53.       // Use the current selection as suggested name
  54.       var name = GetSelectionAsText();
  55.       // Get 40 characters of the selected text and don't add "...",
  56.       //  replace whitespace with "_" and strip non-word characters
  57.       name = ConvertToCDATAString(TruncateStringAtWordEnd(name, 40, false));
  58.       //Be sure the name is unique to the document
  59.       if (AnchorNameExists(name))
  60.         name += "_"
  61.  
  62.       // Make a copy to use for AdvancedEdit
  63.       globalElement = anchorElement.cloneNode(false);
  64.       globalElement.setAttribute("name",name);
  65.     }
  66.   }
  67.   if(!anchorElement)
  68.   {
  69.     dump("Failed to get selected element or create a new one!\n");
  70.     window.close();
  71.     return;
  72.   }
  73.  
  74.   InitDialog();
  75.   
  76.   DoEnabling();
  77.   SetTextboxFocus(gDialog.NameInput);
  78.   SetWindowLocation();
  79. }
  80.  
  81. function InitDialog()
  82. {
  83.   gDialog.NameInput.value = globalElement.getAttribute("name");
  84. }
  85.  
  86. function ChangeName()
  87. {
  88.   if (gDialog.NameInput.value.length > 0)
  89.   {
  90.     // Replace spaces with "_" and strip other non-URL characters
  91.     // Note: we could use ConvertAndEscape, but then we'd
  92.     //  have to UnEscapeAndConvert beforehand - too messy!
  93.     gDialog.NameInput.value = ConvertToCDATAString(gDialog.NameInput.value);
  94.   }
  95.   DoEnabling();
  96. }
  97.  
  98. function DoEnabling()
  99. {
  100.   var enable = gDialog.NameInput.value.length > 0;
  101.   SetElementEnabled(gDialog.OkButton,  enable);
  102.   SetElementEnabledById("AdvancedEditButton1", enable);
  103. }
  104.  
  105. function AnchorNameExists(name)
  106. {
  107.   var anchorList = editorShell.editorDocument.anchors;
  108.   if (anchorList) {
  109.     for (var i = 0; i < anchorList.length; i++) {
  110.       if (anchorList[i].name == name)
  111.         return true;
  112.     }
  113.   }
  114.   return false;
  115. }
  116.  
  117. // Get and validate data from widgets.
  118. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  119. function ValidateData()
  120. {
  121.   var name = TrimString(gDialog.NameInput.value);
  122.   if (!name)
  123.   {
  124.       ShowInputErrorMessage(GetString("MissingAnchorNameError"));
  125.       SetTextboxFocus(gDialog.NameInput);
  126.       return false;
  127.   } else {
  128.     // Replace spaces with "_" and strip other characters
  129.     // Note: we could use ConvertAndEscape, but then we'd
  130.     //  have to UnConverAndEscape beforehand - too messy!
  131.     name = ConvertToCDATAString(name);
  132.  
  133.     if (originalName != name && AnchorNameExists(name))
  134.     {
  135.       ShowInputErrorMessage(GetString("DuplicateAnchorNameError").replace(/%name%/,name));            
  136.       SetTextboxFocus(gDialog.NameInput);
  137.       return false;
  138.     }
  139.     globalElement.setAttribute("name",name);
  140.   }
  141.   return true;
  142. }
  143.  
  144. function onAccept()
  145. {
  146.   if (ValidateData())
  147.   {
  148.     if (originalName != globalElement.name)
  149.     {
  150.       // Copy attributes to element we are changing or inserting
  151.       editorShell.CloneAttributes(anchorElement, globalElement);
  152.  
  153.       if (insertNew) {
  154.         // Don't delete selected text when inserting
  155.         try {
  156.           editorShell.InsertElementAtSelection(anchorElement, false);
  157.         } catch (e) {
  158.           dump("Exception occured in InsertElementAtSelection\n");
  159.         }
  160.       }
  161.     }
  162.     SaveWindowLocation();
  163.     return true;
  164.   }
  165.   return false;
  166. }
  167.